GeneralTest.php

<?php

namespace Tlf\Scrawl\Test;


/**
 * The GeneralTest class handles most tests of this library. This key is here purely to test the @export feature
 *
 * @export(Test.GeneralTest)
 */
class GeneralTest extends \Taeluf\Tester {

    protected $src;
    protected $dest;
    protected $pwd;
    protected $keys;

    // generate documentation. Our tests will check against the output
    public function prepare(){
        $pwd = dirname(__DIR__);
        $this->pwd = $pwd; 
        $this->src = $pwd.'/docs-src';
        $this->dest = $pwd.'/docs';

        $cli = new \Tlf\Scrawl\Cli($pwd, ['--noprompt']);
        $cli->run();

        $keysFile = $this->dest.'/exports/keys.php';
        $this->keys = require($keysFile);
    }

    public function testOutputFileContainsExportedKey(){
        $file = $this->dest.'/TechnicalStuff.md';
        $content = file_get_contents($file);
        if (strpos($content,'if (!$dryRun&&$this->configs[\'deleteExistingDocs\'][0]===true){')!==false){
            return true;
        }
    
        return false;
    }

    public function testAllExportedKeysArePresent(){
        $testKeys = [
            'Configs.defaultsCode',
            'Configs.list',
            'Example.DocBlockExport',
            'Extensions.CustomCall',
            'Extensions.EventsList',
            'Regex.infoArray',
            'Regex.invoke',
            'Regex.onMatchBlock',
            'Regex.onMatchFull',
            'Regex.structure',
            'Technical.deleteExistingDocs.sanityCheck',
            'Test.GeneralTest',
            'Test.GeneralTest.testKeysExported',
        ];
        $realKeys = array_keys($this->keys);
        sort($testKeys);
        sort($realKeys);
        print_r($realKeys);

        
        //  @export_start(Example.DocBlockExport)
        /**
         * This is a docblock export, here for testing purposes.
         * @export(Test.GeneralTest.testKeysExported)
         */
        // @export_end(Example.DocBlockExport)
        return true
            && $this->test('@export() Docblock is correct')
                && $this->compare('This is a docblock export, here for testing purposes.', $this->keys['Test.GeneralTest.testKeysExported'])
            && $this->test('@export_start, @export_end content is correct')
                && $this->compare("/**\n * This is a docblock export, here for testing purposes."
                                    ."\n * @export(Test.GeneralTest.testKeysExported)\n */"
                                , $this->keys['Example.DocBlockExport'])
            && $this->test('All exported keys are found')
                && $this->compare($testKeys, $realKeys)
        ;
    
    }

    public function testFilesWritten(){
        $srcList = [];
        foreach (\Tlf\Scrawl\Utility::allFilesFromDir($this->src, '', ['.src.md']) as $file){
            $srcList[] = substr($file->relPath, 0, -strlen('.src.md')).'.md';
        }
        
        $destList = [];
        foreach (\Tlf\Scrawl\Utility::allFilesFromDir($this->dest, '', ['.md']) as $file){
            $destList[] = $file->relPath;
        }

        // Add auto generated .md files to the srcList
        $srcList[] = '/exports/keys.md';
        
        sort($destList);
        sort($srcList);
    
        return true
            && $this->compare($destList, $srcList)
        ;
    }

}

GeneralTest::runAll();